<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="120px">
      <el-form-item label="ONVIF账号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入ONVIF登录账号"
          clearable
        />
      </el-form-item>
      <el-form-item label="ONVIF密码" prop="password">
        <el-input type="password"
          v-model="queryParams.password"
          placeholder="请输入ONVIF登录密码"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-video-play" size="mini" @click="handleStart" v-if="!timer">开始搜索</el-button>
        <el-button type="primary" icon="el-icon-video-pause" size="mini" @click="handleStop" v-else>停止搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleAdd"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-refresh-left"
          size="mini"
          @click="getList"
        >刷新</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="cameraList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="代码" align="center" prop="deviceId" />
      <el-table-column label="IP" align="center" prop="ip" />
      <el-table-column label="厂商" align="center" prop="manufacturer" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="RTSP" align="center" prop="rtsp" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.token"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >添加</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listCamera, addCamera, startSearch, stopSearch } from "@/api/video/onvif";

export default {
  name: "Camera",
  dicts: ['sys_common_status', 'camera_type', 'sys_yes_no'],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 摄像机表格数据
      cameraList: [],
      // 查询参数
      queryParams: {
        username: null,
        password: null
      },
      // 定时
      timer: null,
      times:0,
    };
  },
  created() {
    this.getList();
  },
  unmounted() {
    this.handleStop();
  },
  methods: {
    /** 查询摄像机列表 */
    getList() {
      this.loading = true;
      listCamera().then(response => {
        this.cameraList = response.data;
        this.loading = false;

        //100次调用后，自动停止
        this.times = this.times + 1;
        if(this.times > 100){              
          clearInterval(this.timer);
          this.timer = null;
          this.times = 0;
        }
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ip)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 启动按钮操作 */
    handleStart() {
      startSearch(this.queryParams).then(response => {
        this.$modal.msgSuccess("启动成功，请等待");
        let that = this;
        this.timer = setInterval(function(){
          that.getList();
        }, 3000);
      });
    },
    
    /** 停止按钮操作 */
    handleStop() {
      stopSearch().then(response => {
        this.$modal.msgSuccess("停止成功，请等待");        
        clearInterval(this.timer);
        this.timer = null;
      });
    },
    /** 启动按钮操作 */
    handleAdd(row) {
      const id = row ?  [row.ip] : this.ids
      addCamera(id).then(response => {
        this.$modal.msgSuccess("添加摄像机成功");
        clearInterval(this.timer);
        this.timer = null;
      });
    }
  }
};
</script>
